package com.c.yinyuezhushou.Dialog;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.c.yinyuezhushou.Adapter.MusicRecAdapter;
import com.c.yinyuezhushou.Interface.MyInterface;
import com.c.yinyuezhushou.MyApplication;
import com.c.yinyuezhushou.OnScrollListener.MusicRecOnScrollListener;
import com.c.yinyuezhushou.R;
import com.c.yinyuezhushou.MRetrofit.MusicItem;
import com.c.yinyuezhushou.Service.MusicService;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import java.util.ArrayList;
import java.util.List;

public class LsListDialog extends BottomSheetDialogFragment {

    private List<MusicItem> list;
    private List<MusicItem> ylist;
    private RecyclerView recyclerView;
    private MusicService musicService;

    public LsListDialog(List<MusicItem> list,MusicService musicService) {
        ylist=list;
        this.list = new ArrayList<MusicItem>();
        this.list.addAll(list);
        this.musicService = musicService;
    }

    public MusicService getMusicService() {
        return musicService;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置背景透明，才能显示出layout中诸如圆角的布局，否则会有白色底（框）
        setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.dialog_heme_listt,container,false);

        recyclerView = view.findViewById(R.id.dhl_rec);
        TextView textView = view.findViewById(R.id.dhl_text);

        textView.setText(getTag());

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);
        MusicRecAdapter recAdapter = new MusicRecAdapter(list, musicService, getFragmentManager(), new MyInterface() {
            @Override
            public void con() {

            }

            @Override
            public void can() {

            }

            @Override
            public void musicItem(MusicItem musicItem) {
                MyApplication myApplication = (MyApplication)getContext().getApplicationContext();
                MyDialog myDialog = new MyDialog(getContext(), true, null, "是否移除？", null ,new MyInterface() {
                    @Override
                    public void con() {
                        ylist.remove(musicItem);
                        if (myApplication.getMyDButil().removeMusicItemTable(musicItem,"LsMusicItem")){
                            Toast.makeText(myApplication, "移除成功", Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(myApplication, "移除失败", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void can() {

                    }

                    @Override
                    public void musicItem(MusicItem musicItem) {

                    }
                });
                myDialog.show();
            }
        });
        recyclerView.setAdapter(recAdapter);
        return view;
    }
}
